<?php
namespace app\admin\controller;

use app\admin\model\NodeModel;
use app\admin\model\MsgModel;
use app\admin\model\UserModel;
use app\admin\model\WebsetModel;
class Index extends Base
{
    public function index(){
        // 获取权限菜单
        $node = new NodeModel();
		// msg_list
		$msgModel = new MsgModel();
		$userModel = new UserModel();
		$websetModel = new WebsetModel();
		$webInfo = $websetModel->getInfo();

		$UserMsg['list'] = $msgModel->where([
			'user_ids' => ['like',["%|".session('id')."|%"]],
		])->order('update_at desc')->field([
			'id,msg_name,last_user_id,read_ids,last_msg,update_at'
		])->select()->toArray();
		foreach($UserMsg['list'] as &$v){
			if($v['last_user_id'] == 0){
				$v['user_info'] = [
					'real_name' => $webInfo['title'],
					'head' => $webInfo['mini_logo'],
				];
			}else{
				$v['user_info'] = $userModel->where('id',$v['last_user_id'])->field('real_name,head')->find();
			}
			$v['read_ids'] = explode(',',$v['read_ids']);
			if(in_array("|".session('id')."|",$v['read_ids'])){
				$v['is_read'] = 1;
			}else{
				$v['is_read'] = 0;
			}
			$v['update_at'] = date("Y-m-d H:i");
		}
		$UserMsg['count'] = $msgModel->where([
			'read_ids' => ['not like',["%|".session('id')."|%"]],
			'user_ids' => ['like',["%|".session('id')."|%"]],
		])->count();
        $this->assign([
            'menu' => $node->getMenu(session('rule')),
			'MsgList' => $UserMsg
        ]);
        return $this->fetch('/index');
    }

    /**
     * [后台默认首页]
     */
    public function indexPage(){
        return $this->fetch('index');
    }
}
